package cn.lamplet.util.kccu;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


public class ParseId {
    private final Logger logger = LoggerFactory.getLogger(ParseId.class);
    private String segFileName;
    private String paraFileName;
    private String hexFileName;
    private String canId;
    private ArrayList<String> hardwareId;
    private ArrayList<String> hexFileList;
    private String partNumber;
    private String reserve;

    public ParseId() {
        hardwareId = new ArrayList<>();
        hexFileList = new ArrayList<>();

    }

    public ParseId(InputStream inputStream) {
        hardwareId = new ArrayList<>();
        hexFileList = new ArrayList<>();
        try {
            List<String> lines = IOUtils.readLines(inputStream, StandardCharsets.UTF_8);
            for (String line : lines) {
                handleLine(line);
            }
        } catch (Exception e) {
            logger.error("解析par文件异常", e);
        }
    }

    public String getSegFileName() {
        return segFileName;
    }

    public String getParaFileName() {
        return paraFileName;
    }

    public String getHexFileName() {
        return hexFileName;
    }

    public String getCanId() {
        return canId;
    }

    public ArrayList<String> getHardwareId() {
        return hardwareId;
    }

    public ArrayList<String> getHexFileList() {
        return hexFileList;
    }

    public String getPartNumber() {
        return partNumber;
    }
    public String getReserve() {
        return reserve;
    }

    public int handleId(String dir, String file_name) {
        File file = new File(dir, file_name);
        String line;
        int ret;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
                handleLine(line);
            }
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return 0;
    }

    private int handleLine(String line) {
//      if (line.charAt(0) == '#') return -1;
        String[] str = line.split(":");
        if (str.length == 1) {
            System.out.println(str.length + " ****** comment ******");
        } else {
//              System.out.println(str[0].trim()+" "+str[1].trim());
            switch (str[0].trim()) {
                case "seg":
                    System.out.println("seg: " + str[1].trim());
                    segFileName = str[1].trim();
                    break;
                case "par":
                    System.out.println("par: " + str[1].trim());
                    paraFileName = str[1].trim();
                    break;
                case "hex":
                    System.out.println("hex: " + str[1].trim());
//                    hexFileName = str[1].trim();
                    String[] str2 = str[1].trim().split(",");
                    for (int i = 0; i < str2.length; i++) {
//                      System.out.println(str1[i]);
                        hexFileList.add(str2[i].trim());
                    }
                    break;
                case "can_id":
                    System.out.println("can_id: " + str[1].trim());
                    canId = str[1].trim();
                    canId=canId.substring(canId.length()-3);
                    System.out.println(canId+" "+canId.length());
                    break;
                case "hardware_id":
                    System.out.println("hardware_id: " + str[1].trim());
                    String[] str1 = str[1].trim().split(",");
                    for (int i = 0; i < str1.length; i++) {
//                      System.out.println(str1[i]);
                        hardwareId.add(str1[i].trim());
                    }

                    break;
                case "part_number":
                    System.out.println("part_number: " + str[1].trim());
                    partNumber = str[1].trim();
                    break;
                case "reserve":
                    System.out.println("reserve: " + str[1].trim());
                    reserve = str[1].trim();
                    break;
            }
        }
        return 0;
    }

    private int handleLine2(String line) {
        if (line.charAt(0) == '#') return -1;
        String[] str = line.split(";");
        for (int i = 1; i < str.length; i += 3) {
            if (str[i].equals("#")) {
                break;
            }
            //     System.out.println(str[i + 1].substring(2) + ":" + str[i + 2]);
        }
        segFileName = str[1];
        paraFileName = str[2];
        hexFileName = str[3];
        canId = str[4];
//        hardwareId = str[5];
        partNumber = str[6];
        System.out.println(segFileName + " " + paraFileName + " " + hexFileName
                + " " + canId + " " + hardwareId + " " + partNumber);
        return 0;
    }
}
